#!/bin/bash

fail_build ()
{
	echo BUILD FAILED
	exit 1
}

rm -rf target
mkdir target

main_jars=`find war/WEB-INF/lib -name *.jar -exec echo -n {}: \;`
test_jars=`find lib/test -name *.jar`

mkdir -p target/main/classes
javac -target 1.5 -d target/main/classes -cp $main_jars. `find src/main/java -name *.java`
if [ $? -ne 0 ]; then
	fail_build
fi

mkdir -p target/test/classes
javac -target 1.5 -d target/test/classes -cp $main_jars:$test_jars:target/main/classes -encoding utf8 `find src/test/java -name *.java`
if [ $? -ne 0 ]; then
	fail_build
fi

java -cp $main_jars:$test_jars:target/main/classes:target/test/classes org.junit.runner.JUnitCore `cd src/test/java; find . -name *Test.java | sed s@/@.@g | sed s/.java$//g | sed s/\\\.\\\.//g`
if [ $? -ne 0 ]; then
	fail_build
fi

echo BUILD SUCCESSFUL
exit 0